package echecs.ia;

import echecs.launch.ChessInterface;
import echecs.run.Run;
import javax.swing.JApplet;

public abstract class Evaluation extends Run {
    //move of the AI
    //variables for the evaluation

    static float[] posvalues = {0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.01F, 0.02F, 0.03F, 0.03F, 0.02F, 0.01F, 0.0F, 0.0F, 0.0F, 0.01F, 0.04F, 0.04F, 0.04F, 0.04F, 0.04F, 0.04F, 0.01F, 0.0F, 0.0F, 0.03F, 0.04F, 0.06F, 0.06F, 0.06F, 0.06F, 0.04F, 0.02F, 0.0F, 0.0F, 0.03F, 0.04F, 0.06F, 0.08F, 0.08F, 0.06F, 0.04F, 0.03F, 0.0F, 0.0F, 0.03F, 0.04F, 0.06F, 0.08F, 0.08F, 0.06F, 0.04F, 0.03F, 0.0F, 0.0F, 0.02F, 0.04F, 0.06F, 0.06F, 0.06F, 0.06F, 0.04F, 0.02F, 0.0F, 0.0F, 0.01F, 0.04F, 0.04F, 0.04F, 0.04F, 0.04F, 0.04F, 0.01F, 0.0F, 0.0F, 0.0F, 0.01F, 0.02F, 0.03F, 0.03F, 0.02F, 0.01F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F};
    public boolean ababort = false;

    public Evaluation(JApplet ref) {
        super(ref);
    }

    //evaluate a position
    public float evaluation() {
        float valeur = 0;
        float figur = 0;
        for (int i = 21; i < 99; i++) {
            if (board[i] != 0) {
                //material
                switch (board[i] % 10) {
                    case 1:
                        figur = 1.0F;
                        break;
                    case 2:
                    case 3:
                        figur = 3.0F;
                        break;
                    case 4:
                        figur = 4.5F;
                        break;
                    case 5:
                        figur = 9.0F;
                        break;
                    case 6:
                        figur = 0.0F;
                }
                //position
                figur += posvalues[i];
                if (board[i] % 100 / 10 == color) {
                    valeur += figur;
                } else {
                    valeur -= figur;
                }
            }
            if (i % 10 == 8) {
                i += 2;
            }
        }
        return valeur;
    }

    public void postgenmove() {
        deep--;
        ababort = false;
    }

    public void pregenmove() {
        deep++;
        ababort = false;
        //find checkmat and initialize alphabeta
        if (deep % 2 != 0) {
            ChessInterface.minimax[deep] = 2000.0F;
            ChessInterface.alphabeta[deep] = 3000.0F;
        } else {
            //human
            ChessInterface.minimax[deep] = -2000.0F;
            ChessInterface.alphabeta[deep] = -3000.0F;
        }
    }

    public void changeColor() {
        if (color == 1) {
            color = 2;
        } else {
            color = 1;
        }
    }

    public void nodeValue() {
        //calculate value of this node
        if (target == deep) {
            value = evaluation();
        } else {
            changeColor();
            genmove();
            value = ChessInterface.minimax[deep + 1];
            //change alpha beta field?
            if (deep % 2 != 0) {
                if (value < ChessInterface.alphabeta[deep]) {
                    ChessInterface.alphabeta[deep] = value;
                }
            } else {
                //human
                if (value > ChessInterface.alphabeta[deep]) {
                    ChessInterface.alphabeta[deep] = value;
                }
            }
            changeColor();
        }
    }
}
